<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'edit';
$merch=model('jy_merch/model')->checkmerch();
$this->model('model')->checkvip();
if($_RQ['type']){
	$this->session('type',$_RQ['type']);
}
$type=$this->session('type');
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_shopbaojia_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));		
	}else{
		$item=array('type'=>$this->session('type'));
	}
	$return['item']=$item;
	$return['groups']=pdo_getall('jy_shopbaojia_group',array('uniacid'=>$_SESSION['uniacid'],'pid'=>0),array('id','title'),'id');
	foreach($return['groups'] as &$gv){
		$gv['cids']=pdo_getall('jy_shopbaojia_group',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$gv['id']),array('id','title'),'id');
	}
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['merchid']){
		//exi('请选择发布的商户！','error');
	}
	if(!$postdata['title']){
		exi('请填写商品名称！','error');
	}
	if(!$postdata['productsn']){
		//exi('请填写商品编码！','error');
	}
	if(!$postdata['image']){
		//exi('请填写商品图片！','error');
	}
	if(!$postdata['price']){
		exi('请填写报价！','error');
	}
	if(!$postdata['group_id']){
		//exi('请填写商品分类！','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'image'=>trim($postdata['image']),
		'desc'=>trim($postdata['desc']),
		'cid'=>intval($postdata['cid']),
		'group_id'=>intval($postdata['group_id']),
		'price'=>trim($postdata['price']),
		'brand'=>trim($postdata['brand']),
		'unit'=>trim($postdata['unit']),
		'optiontitle'=>trim($postdata['optiontitle']),
		'productsn'=>trim($postdata['productsn']),
		'noshow'=>intval($postdata['noshow']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_shopbaojia_list',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','credit4','credit6','mobile','avatar','nickname'));
		if($this->setting['kan_upfee'] > $member['credit3']){
			exi('您的金币不足，不能参与！','error');
		}
		if($this->setting['kan_upfee']>0){
			$data['status']=1;
			model('core/member')->member_credit($_SESSION['uid'],'credit3',$this->setting['kan_upfee'],false,'jy_shopbaojia','发布报价扣金币');
		}
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=intval($merch['id']);
		$data['createtime']=TIMESTAMP;
		$data['updatetime']=TIMESTAMP;
		pdo_insert('jy_shopbaojia_list',$data);
	}
	exi('操作成功！','','jy_shopbaojia/mylist/main/');
}
if($_RQ['op']=='getproductsn'){
	$return=array();
	$title=trim($_RQ['title']);
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'noshow'=>0,'productsn <>'=>'');	
	if($title){
		$params['title like']='%'.$title.'%';
		$productsn=pdo_getcolumn('jy_shopbaojia_list',$params,'productsn');		
	}else{
		exi('none');
	}
	$productsn=$productsn?:'none';
	exi($productsn);
}